package heima.lm.oschina.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.R;
import heima.lm.oschina.bean.Tweet;
import heima.lm.oschina.bean.User;

/**
 * Created by herenxue on 2017/4/6.
 */

public class HotTweetRvAdapter extends BaseRecyclerViewAdapter {

    public HotTweetRvAdapter(List list) {
        super(list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(BaseApplication.mApp).inflate(R.layout.item_rv_latest_tweet, parent, false);
        return new LatestTweetViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        LatestTweetViewHolder ltHolder = (LatestTweetViewHolder) holder;
        Tweet tweet = (Tweet) mList.get(position);
        /*Glide.with(BaseApplication.mApp).load(
                HttpUtils.getPortraitUrl(tweet.getPortrait())).into(ltHolder.mIvUserIcon);*/
        ltHolder.mIvUserIcon.setImageResource(R.drawable.icon_actionbar_home);
        ltHolder.mTvTweetTitle.setText(tweet.getAuthor());
        Spanned body = Html.fromHtml(tweet.getBody());
        ltHolder.mTvTweetContent.setMovementMethod(LinkMovementMethod.getInstance());
        ltHolder.mTvTweetContent.setText(body);
        ltHolder.mTvTweetDate.setText(tweet.getPubDate());
        ltHolder.mIvTweetFavour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnim(v);
            }
        });
        List<User> likeUser = tweet.getLikeUser();
        StringBuilder sb = new StringBuilder();
        for (User user : likeUser) {
            String name = user.getName();
            Spanned spanned = Html.fromHtml(r(name));
            sb.append(spanned).append("、");
        }
        sb.append("觉得很赞");
        ltHolder.mTvTweetFavourUsers.setMovementMethod(LinkMovementMethod.getInstance());
        ltHolder.mTvTweetFavourUsers.setText(sb.toString());
        ltHolder.mTvTweetCommentCount.setText(tweet.getCommentCount());
    }

    public static String r(String source) {
        //对字符串设置格式  ，%s：占位符
        return String.format("<font color='#66C5E7'>%s</font>", source);
    }

    private void startAnim(View view) {
        ScaleAnimation sa = new ScaleAnimation(1.0f, 1.4f, 1.0f, 1.4f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(1000);
        view.startAnimation(sa);
    }

    public static class LatestTweetViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_latest_tweet)
        ImageView mIvUserIcon;
        @BindView(R.id.tv_title_latest_tweet)
        TextView mTvTweetTitle;
        @BindView(R.id.tv_content_latest_tweet)
        TextView mTvTweetContent;
        @BindView(R.id.tv_date_latest_tweet)
        TextView mTvTweetDate;
        @BindView(R.id.iv_favour_latest_tweet)
        ImageView mIvTweetFavour;
        @BindView(R.id.tv_comment_count_latest_tweet)
        TextView mTvTweetCommentCount;
        @BindView(R.id.tv_favor_user_latest_tweet)
        TextView mTvTweetFavourUsers;


        public LatestTweetViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
